package com.example.github.networking;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpURLConnectionDemo {
    public static void main(String[] args) {
        /**
         * 1. 通过URL 创建实例url
         * 2. 打开url 连接
         * 3. 将打开的连接对象转换成 HttpURLConnection 连接对象
         * 4. 输出对应信息
         * 5. 关闭连接
         */
        try {
            URL url = new URL("https://www.javatpoint.com/java-http-url-connection");
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpCon =  (HttpURLConnection) urlConnection;
            for (int i = 1; i <= 8; i++) {
                System.out.println(httpCon.getHeaderFieldKey(i) + ": " +httpCon.getHeaderField(i));
            }
            httpCon.disconnect();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
